package com.pinyoumei.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.pinyoumei.app.R;
import com.pinyoumei.app.bean.ChooseCouponBean;
import com.pinyoumei.app.bean.EPayBean;
import com.pinyoumei.app.bean.OrderPayBean;
import com.pinyoumei.app.bean.OrderSubmitBean;
import com.pinyoumei.app.bean.WxPayEntity;
import com.pinyoumei.app.network.Constants;
import com.pinyoumei.app.utils.AppManager;
import com.pinyoumei.app.utils.BigDecimalUtils;
import com.pinyoumei.app.utils.EventBusUtils;
import com.pinyoumei.app.utils.PayUtils;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.SignatureUtils;
import com.pinyoumei.app.utils.StatusBarUtils;
import com.pinyoumei.app.utils.ToastUtils;
import com.pinyoumei.app.utils.Utils;
import com.pinyoumei.app.utils.event.AlipayEvent;
import com.pinyoumei.app.utils.event.WechatPay;
import com.pinyoumei.app.view.LoadingDailog;
import com.pinyoumei.app.view.dialog.BottomDialog;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2021-08-10.
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2021-08-10        Administrator             新增Person类
 * 2021-08-10        Administrator             增加sex属性
 * 确定订单
 */
public class OrderPayActivity extends AppCompatActivity implements View.OnClickListener {

    private int goods_num;
    private String goods_id;
    private int order_type;
    private TextView tv_youhui;
    private TextView tv_total_price;
    private TextView redPackageAmount;
    private TextView tv_title;
    private TextView tv_size;
    private TextView tv_num;
    private ImageView iv_1;
    private String total_price="";
    private String red_package_id = "";
    private TextView tv_coupon_desc;
    private RelativeLayout relative_confirm_order;//新增收货地址点击区域
    private RelativeLayout relative_addressmanagement;//当用户已经有默认收货地址的时候该显示的点击区域
    private TextView text_address;//地址
    private TextView text_address_username;//收货人
    private TextView text_address_userphone;//收货人电话
    private Boolean have_address = true;//用户是否有默认收货地址 默认为false
    private String address_id = "";//当前默认地址的address_id
    private String coupon_id = "";
    private String pay_type = "2";
    private OrderSubmitBean orderSubmitBean;

    protected AppManager appManager = AppManager.getInstance();
    private Gson gson;
    private LoadingDailog loadingDailog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //当FitsSystemWindows设置 true 时，会在屏幕最上方预留出状态栏高度的 padding
        StatusBarUtils.setRootViewFitsSystemWindows(this,true);
        //设置状态栏透明
        StatusBarUtils.setTranslucentStatus(this);
        //一般的手机的状态栏文字和图标都是白色的, 可如果你的应用也是纯白色的, 或导致状态栏文字看不清
        //所以如果你是这种情况,请使用以下代码, 设置状态使用深色文字图标风格, 否则你可以选择性注释掉这个if内容
        if (!StatusBarUtils.setStatusBarDarkTheme(this, true)){
            //如果不支持设置深色风格 为了兼容总不能让状态栏白白的看不清, 于是设置一个状态栏颜色为半透明,
            //这样半透明+白=灰, 状态栏的文字能看得清
            StatusBarUtils.setStatusBarColor(this,0x55000000);
        }
        // 添加activity 到栈
        appManager.addActivity(this);

        gson = new Gson();
        LoadingDailog.Builder builder = new LoadingDailog.Builder(this)
                .setMessage("加载中...")
                .setCancelable(true);
        loadingDailog = builder.create();

        setContentView(R.layout.activity_order_pay);
        EventBusUtils.register(this);
        goods_id = getIntent().getStringExtra("goods_id");
        goods_num = getIntent().getIntExtra("goods_num",1);
        order_type = getIntent().getIntExtra("order_type",0);


        InitWidget();

        if (!TextUtils.isEmpty(goods_id)){
            InitData();
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN, priority = 1)
    public void onMessageEventMain(WechatPay event) {
        if (event != null ) {
            //订单详情
            if (orderSubmitBean!=null && !TextUtils.isEmpty(orderSubmitBean.getResult().getOrder_id())){
                Intent intent = new Intent(OrderPayActivity.this,ZBkOrderDetailActivity.class);
                intent.putExtra("order_id",orderSubmitBean.getResult().getOrder_id());
                startActivity(intent);
                finish();
                appManager.killActivity(ProductDetailActivity.class);
                appManager.killActivity(ProductDetailPtActivity.class);
            }

        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, priority = 1)
    public void onMessageEventMain(String wxPay) {
        if (TextUtils.equals(wxPay,"wx_finsh")) {
           finish();
        }
    }

    private void InitWidget() {
        RelativeLayout relative_finish = findViewById(R.id.relative_finish);
        relative_finish.setOnClickListener(this);
        RelativeLayout rl_yhq = findViewById(R.id.rl_yhq);
        rl_yhq.setOnClickListener(this);

        iv_1 = findViewById(R.id.iv_1);

        tv_youhui = findViewById(R.id.tv_youhui);
        tv_total_price = findViewById(R.id.tv_total_price);
        redPackageAmount = findViewById(R.id.red_package_amount);
        tv_title = findViewById(R.id.tv_title);
        tv_size = findViewById(R.id.tv_size);
        tv_num = findViewById(R.id.tv_num);


        text_address = findViewById(R.id.text_address);
        text_address_username = findViewById(R.id.text_address_username);
        text_address_userphone = findViewById(R.id.text_address_userphone);
        relative_addressmanagement = findViewById(R.id.relative_addressmanagement);
        relative_addressmanagement.setOnClickListener(this);
        relative_confirm_order = findViewById(R.id.relative_confirm_order);
        relative_confirm_order.setOnClickListener(this);


        Button bt_sure_buy = findViewById(R.id.bt_sure_buy);
        bt_sure_buy.setOnClickListener(this);
    }

    private void InitData() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(OrderPayActivity.this));
        map.put("goods_id", goods_id);
        map.put("order_type", order_type);
        map.put("buy_num", goods_num);


        OkGo.<String>post(Constants.GOODS_ORDER_INIT)
                .params("user_id", SPUtils.getAppUserId(OrderPayActivity.this))
                .params("goods_id", goods_id)
                .params("order_type", order_type)
                .params("buy_num", goods_num)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        setData(response);
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();
                        OrderPayBean orderPayBean = gson.fromJson(response.body(), OrderPayBean.class);

                        if (orderPayBean.getCode() >= 0) {
                            //地址id
                            if (orderPayBean.getResult().getDefault_address().getId() == null) {//如果该对象为空
                                have_address = false;
                                relative_confirm_order.setVisibility(View.VISIBLE);
                                relative_addressmanagement.setVisibility(View.GONE);
                            } else { //有默认地址
                                have_address = true;
                                relative_confirm_order.setVisibility(View.GONE);
                                relative_addressmanagement.setVisibility(View.VISIBLE);
                                text_address.setText(orderPayBean.getResult().getDefault_address().getAddress_area()
                                        + orderPayBean.getResult().getDefault_address().getAddress_detail());
                                text_address_username.setText(orderPayBean.getResult().getDefault_address().getRecieve_name());
                                text_address_userphone.setText(orderPayBean.getResult().getDefault_address().getRecieve_phone());
                                //默认地址id赋值
                                address_id = orderPayBean.getResult().getDefault_address().getId();
                            }


                            Glide.with(OrderPayActivity.this).load(orderPayBean.getResult().getGoods_info().getGoods_img()).into(iv_1);
                            tv_title.setText(orderPayBean.getResult().getGoods_info().getGoods_name());
                            tv_size.setText("¥" +orderPayBean.getResult().getGoods_info().getGoods_price());
                            tv_num.setText("x" +orderPayBean.getResult().getGoods_info().getBuy_num());

                            tv_youhui.setText(orderPayBean.getResult().getCoupon_desc());
                            total_price = orderPayBean.getResult().getTotal_price()+"";
                            DecimalFormat df2 =new DecimalFormat("0.00");
                            String str2 = df2.format(Double.parseDouble(total_price));
                            tv_total_price.setText(str2);
                            redPackageAmount.setText("-￥"+orderPayBean.getResult().getRed_package_amount());
                            red_package_id = orderPayBean.getResult().getRed_package_id();
                        } else {
                            Toast.makeText(OrderPayActivity.this, orderPayBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }

                    }

                });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        	case R.id.relative_finish :
        		finish();
        		break;

            case R.id.relative_confirm_order://点击新增收货地址
                Intent i = new Intent(OrderPayActivity.this, ModificationAddressActivity.class);
                i.setAction("ConfirmOrderActivity");
                startActivityForResult(i, 1000);
                break;

            case R.id.relative_addressmanagement://收货地址管理
                Intent intent = new Intent(OrderPayActivity.this, AddressManagementActivity.class);
                intent.putExtra("id", address_id);
                intent.setAction("ConfirmOrderActivity");
                startActivityForResult(intent, 1000);
                break;


        		case R.id.rl_yhq :
        		//优惠券
                    Intent intent2 = new Intent(OrderPayActivity.this, CouponChooseActivity.class);
                    intent2.putExtra("total_price", total_price);
                    startActivityForResult(intent2, 1200);
                    break;
	        case R.id.bt_sure_buy :
                //提交订单
                if (TextUtils.isEmpty(address_id)) {
                    Toast.makeText(OrderPayActivity.this, "还没有地址，请去添加地址", Toast.LENGTH_SHORT).show();
                } else {

                   SubmitData();
                }



        		break;

        	default:
        		break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == 1000 && resultCode == 2000) {
            String id = data.getStringExtra("id");
            address_id = id;
            String recieve_name = data.getStringExtra("recieve_name");
            text_address_username.setText(recieve_name);
            String recieve_phone = data.getStringExtra("recieve_phone");
            text_address_userphone.setText(recieve_phone);
            String address_area = data.getStringExtra("address_area");
            String address_detail = data.getStringExtra("address_detail");
            text_address.setText(address_area + address_detail);
            String is_default = data.getStringExtra("is_default");
            relative_confirm_order.setVisibility(View.GONE);
            relative_addressmanagement.setVisibility(View.VISIBLE);
            have_address = true;
        }


        //优惠券
        if (requestCode == 1200 && resultCode == 12000) {
            coupon_id = data.getStringExtra("coupon_id");


            if (total_price!=null){

                String deduct_amount = data.getStringExtra("deduct_amount");
                //减法
                Double subtext  = BigDecimalUtils.subtract(Double.parseDouble(total_price), Double.parseDouble(deduct_amount), 1);
                tv_total_price.setText(subtext+"");

                tv_youhui.setText("-" + deduct_amount + "元");
                tv_youhui.setTextColor(ContextCompat.getColor(OrderPayActivity.this,R.color.red));
            }

        }
    }


    private void SubmitData() {

        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(OrderPayActivity.this));
        map.put("goods_id", goods_id);
        map.put("order_type", order_type);
        map.put("buy_num", goods_num);
        map.put("coupon_id", coupon_id);
        map.put("address_id", address_id);
        map.put("red_package_id",red_package_id);

        OkGo.<String>post(Constants.ORDER_SUBMIT)
                .params("user_id", SPUtils.getAppUserId(OrderPayActivity.this))
                .params("goods_id", goods_id)
                .params("order_type", order_type)
                .params("buy_num", goods_num)
                .params("coupon_id", coupon_id)
                .params("address_id", address_id)
                .params("red_package_id", red_package_id)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    private List<ChooseCouponBean.ResultBean.UsableCouponListBean> disable_coupon_list;
                    private List<ChooseCouponBean.ResultBean.UsableCouponListBean> usable_coupon_list;

                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }


                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);

                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();

                        orderSubmitBean = gson.fromJson(response.body(), OrderSubmitBean.class);
                        if (orderSubmitBean.getCode() >= 0) {




                            BottomDialog bottomDialog = new BottomDialog(OrderPayActivity.this,R.layout.dialog_order_pay);
                            bottomDialog.show();
                            pay_type = "2";
                            TextView text_realpay_money = bottomDialog.findViewById(R.id.text_realpay_money);
                            text_realpay_money.setText(orderSubmitBean.getResult().getTotal_price()+"");
                            TextView textRealpayIcon = bottomDialog.findViewById(R.id.text_realpay_icon);
                            TextView text_money = bottomDialog.findViewById(R.id.text_money);
                            text_money.setText("(可用余额：" + orderSubmitBean.getResult().getUser_balance()+")");
                            TextView text_happy_money = bottomDialog.findViewById(R.id.text_happy_money);
                            text_happy_money.setText("(可用余额：" + orderSubmitBean.getResult().getHappy_coupon()+")");

                            CheckBox checkbox__duihuan = bottomDialog.findViewById(R.id.checkbox_duihuan);
                            CheckBox checkbox_zhifubao = bottomDialog.findViewById(R.id.checkbox_zhifubao);
                            CheckBox checkbox_happy = bottomDialog.findViewById(R.id.checkbox_happy);
                            checkbox_zhifubao.setChecked(true);
                            CheckBox checkbox_yue =bottomDialog. findViewById(R.id.checkbox_yue);

                            ImageView  image_cancle =bottomDialog. findViewById(R.id.image_cancle);
                            image_cancle.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    bottomDialog.dismiss();
                                }
                            });

                            RelativeLayout rl_wechat = bottomDialog.findViewById(R.id.rl_wechat);
                            rl_wechat.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    pay_type = "1";
                                    checkbox__duihuan.setChecked(true);
                                    checkbox_zhifubao.setChecked(false);
                                    checkbox_yue.setChecked(false);
                                    checkbox_happy.setChecked(false);
                                    textRealpayIcon.setVisibility(View.VISIBLE);
                                }
                            });

                            RelativeLayout relative_zhifubaopay = bottomDialog.findViewById(R.id.relative_zhifubaopay);
                            relative_zhifubaopay.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    pay_type = "2";
                                    checkbox__duihuan.setChecked(false);
                                    checkbox_zhifubao.setChecked(true);
                                    checkbox_yue.setChecked(false);
                                    checkbox_happy.setChecked(false);
                                    textRealpayIcon.setVisibility(View.VISIBLE);
                                }
                            });

                            RelativeLayout  relative_yuepay = bottomDialog.findViewById(R.id.relative_yuepay);
                            //can_balance_pay  是否可以使用余额支付 1是  0否
//                            switch (orderSubmitBean.getResult().getCan_balance_pay()) {
//                            	case 0:
//                                    relative_yuepay.setVisibility(View.GONE);
//                            		break;
//                                case 1:
//                                    relative_yuepay.setVisibility(View.VISIBLE);
//                                    break;
//                            	default:
//                            		break;
//                            }
                            
                            relative_yuepay.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    pay_type = "3";
                                    checkbox__duihuan.setChecked(false);
                                    checkbox_zhifubao.setChecked(false);
                                    checkbox_yue.setChecked(true);
                                    checkbox_happy.setChecked(false);
                                    textRealpayIcon.setVisibility(View.VISIBLE);
                                }
                            });

                            RelativeLayout relative_happy = bottomDialog.findViewById(R.id.relative_happy);
                            relative_happy.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    pay_type = "4";
                                    checkbox__duihuan.setChecked(false);
                                    checkbox_zhifubao.setChecked(false);
                                    checkbox_yue.setChecked(false);
                                    checkbox_happy.setChecked(true);
                                    textRealpayIcon.setVisibility(View.GONE);
                                }
                            });

                            if(TextUtils.equals(Utils.isActivity,"1")){
                                pay_type = "2";
                                checkbox__duihuan.setChecked(false);
                                checkbox_zhifubao.setChecked(true);
                                checkbox_yue.setChecked(false);
                                checkbox_happy.setChecked(false);
                                rl_wechat.setVisibility(View.VISIBLE);
                                relative_zhifubaopay.setVisibility(View.VISIBLE);
                                relative_yuepay.setVisibility(View.VISIBLE);
                                relative_happy.setVisibility(View.VISIBLE);
                                textRealpayIcon.setVisibility(View.VISIBLE);
                            }else if (TextUtils.equals(Utils.isActivity,"2")){
                                pay_type = "4";
                                checkbox__duihuan.setChecked(false);
                                checkbox_zhifubao.setChecked(false);
                                checkbox_yue.setChecked(false);
                                checkbox_happy.setChecked(true);
                                rl_wechat.setVisibility(View.GONE);
                                relative_zhifubaopay.setVisibility(View.GONE);
                                relative_yuepay.setVisibility(View.GONE);
                                relative_happy.setVisibility(View.VISIBLE);
                                textRealpayIcon.setVisibility(View.GONE);
                            }else if (TextUtils.equals(Utils.isActivity,"3")){
                                pay_type = "1";
                                checkbox__duihuan.setChecked(true);
                                checkbox_zhifubao.setChecked(false);
                                checkbox_yue.setChecked(false);
                                checkbox_happy.setChecked(false);
                                rl_wechat.setVisibility(View.VISIBLE);
                                relative_zhifubaopay.setVisibility(View.VISIBLE);
                                relative_yuepay.setVisibility(View.GONE);
                                relative_happy.setVisibility(View.GONE);
                                textRealpayIcon.setVisibility(View.GONE);
                            }

                            Button button_paymoney = bottomDialog.findViewById(R.id.button_paymoney);
                            button_paymoney.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    bottomDialog.dismiss();

                                    PayData(orderSubmitBean.getResult().getOrder_id());
                                }
                            });

                        } else {
                            ToastUtils.showShort(orderSubmitBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });


    }




    private void PayData(String order_id) {

        Map<String,Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(this));
        map.put("order_id", order_id);
        map.put("pay_type", pay_type);



        OkGo.<String>post( Constants.ORDER_PAY)
                .params("user_id", SPUtils.getAppUserId(this))
                .params("order_id", order_id)
                .params("pay_type", pay_type)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }


                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);
                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();
                        EPayBean agencyRuleBean = gson.fromJson(response.body(), EPayBean.class);
                        if (agencyRuleBean.getCode() > 0 ){
                            switch (pay_type) {
                                case "1":
                                    //微信支付
                                    String otherWxpayData = agencyRuleBean.getResult().getOther_wxpay_data();
                                    if(TextUtils.isEmpty(otherWxpayData)) {
                                        IWXAPI wxapi = WXAPIFactory.createWXAPI(OrderPayActivity.this, SPUtils.getWxKey(OrderPayActivity.this));
                                        PayUtils payUtils = new PayUtils(wxapi, OrderPayActivity.this, agencyRuleBean.getResult().getPartner_id());
                                        WxPayEntity wxPayEntity = new WxPayEntity();
                                        wxPayEntity.setPrepay_id(agencyRuleBean.getResult().getPrepay_id());
                                        wxPayEntity.setNoncestr(agencyRuleBean.getResult().getNonce_str());
                                        wxPayEntity.setTimestamp(agencyRuleBean.getResult().getTime_stamp());
                                        wxPayEntity.setSign(agencyRuleBean.getResult().getSign());
                                        payUtils.payByWechat(wxPayEntity);
                                    }else{
                                        String appId = "wxfcdbc78aac617cee"; // 填应用AppId
                                        IWXAPI api = WXAPIFactory.createWXAPI(OrderPayActivity.this, appId);
                                        WXLaunchMiniProgram.Req req = new WXLaunchMiniProgram.Req();
                                        req.userName = "gh_55a696f51352"; // 填小程序原始id
                                        req.path = "/pages/pay/index?sendMsg="+otherWxpayData;                  //拉起小程序页面的可带参路径，不填默认拉起小程序首页
                                        req.miniprogramType = WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE;// 可选打开 开发版，体验版和正式版
                                        api.sendReq(req);
                                    }
                                    break;
                                case "2":
                                    //支付宝
                                    //需要跳进支付宝H5支付页面
                                    String otherPayData = agencyRuleBean.getResult().getOther_alipay_data();
                                    if(TextUtils.isEmpty(otherPayData)) {
                                        String url = agencyRuleBean.getResult().getAli_pay_url();
                                        if (TextUtils.isEmpty(url)) {
                                            ToastUtils.showShort("支付链接为空");
                                            finish();//一旦出现支付失败订单提交界面得销毁 订单作废 因为不能重复提交同一订单
                                            return;
                                        }
                                        Intent intent1 = new Intent(OrderPayActivity.this, WebViewActivity.class);
                                        intent1.putExtra("url", url);
                                        intent1.putExtra("order_title", "正在支付");
                                        startActivity(intent1);
//                                    h5Pay(url);
                                    }else{
                                        alipay_do(otherPayData);
                                    }
                                    break;
                                case "3":
                                case "4":
                                    Intent intent = new Intent(OrderPayActivity.this, ZBkOrderListActivity.class);
                                    intent.putExtra("tab_type",1);
                                    startActivity(intent);
                                    finish();
                                    appManager.killActivity(ProductDetailActivity.class);
                                    appManager.killActivity(ProductDetailPtActivity.class);

                                    break;
                                default:
                                    break;
                            }

                        }else{
                            ToastUtils.showShort(agencyRuleBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });
    }




    public void h5Pay(String url) {
        Intent intent = new Intent(this, com.alipay.sdk.app.H5PayActivity.class);
        Bundle extras = new Bundle();
        /**
         * url 是要测试的网站，在 Demo App 中会使用 H5PayDemoActivity 内的 WebView 打开。
         *
         * 可以填写任一支持支付宝支付的网站（如淘宝或一号店），在网站中下订单并唤起支付宝；
         * 或者直接填写由支付宝文档提供的“网站 Demo”生成的订单地址
         * （如 https://mclient.alipay.com/h5Continue.htm?h5_route_token=303ff0894cd4dccf591b089761dexxxx）
         * 进行测试。
         *
         * H5PayDemoActivity 中的 MyWebViewClient.shouldOverrideUrlLoading() 实现了拦截 URL 唤起支付宝，
         * 可以参考它实现自定义的 URL 拦截逻辑。
         */
        extras.putString("url", url);
        intent.putExtras(extras);
        startActivity(intent);
        finish();//销毁当前订单确定页面

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBusUtils.unregister(this);
    }

    private void alipay_do(String otherPayData) {
        OkGo.<String>post(Constants.viewPayUrl)
                .params("sendMsg", otherPayData)
                .execute(new StringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);

                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        Intent intent = new Intent(OrderPayActivity.this,OtherWebViewActivity.class);
                        intent.putExtra("includeTitle", "正在支付");
                        intent.putExtra("data",response.getRawResponse().request().url().toString());
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);

                    }

                });
    }
}
